package Thread;

/**
 * @author: lqh
 * @description: 那如何让 两个线程按照指定方式有序交叉运行呢？
 * 我现在希望 A 在打印完 1 后，再让 B 打印 1, 2, 3，最后再回到 A 继续打印 2, 3
 * @program: Demo
 * @create: 2018-04-14 12:52
 **/
public class demo2 {
     static void demo(){
         Object lock = new Object();
         Thread thread_A = new Thread(new Runnable() {
             @Override
             public void run() {
                 synchronized (lock){
                     System.out.println("A print 1");
                     try {
                         lock.wait();
                     } catch (InterruptedException e) {
                         e.printStackTrace();
                     }
                     System.out.println("A print 2");
                     System.out.println("A print 3");
                 }
             }
         });

         Thread thread_B = new Thread(new Runnable() {
             @Override
             public void run() {
                 synchronized (lock){
                     System.out.println("B print 1");
                     System.out.println("B print 2");
                     System.out.println("B print 3");
                     lock.notify();
                 }
             }
         });

         thread_A.start();
         thread_B.start();
     }

    public static void main(String[] args) {
        demo();
    }
}
